home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / misc / amigancp19.lha / AmigaNCP19 / Developer / Source / NCPMon / ncpmon.c < prev    next >
C/C++ Source or Header  |  1996-10-26  |  7KB  |  369 lines

  1. /*
  2. ** AmigaNCP Monitor
  3. ** ----------------
  4. **
  5. ** (C) 1993-1996 Oliver Wagner, All Rights Reserved
  6. **
  7. ** Small'n'ugly hack to monitor NCP activity.
  8. **
  9. */
  10.  
  11. #include <proto/dos.h>
  12. #define __USE_SYSBASE
  13. #include <proto/icon.h>
  14. #include <proto/exec.h>
  15. #include <proto/intuition.h>
  16. #include <proto/graphics.h>
  17. #include <workbench/startup.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <time.h>
  21.  
  22. #include <libraries/ncplib.h>
  23.  
  24. #pragma libcall NCPBase NCP_private1 90 A01
  25.  
  26. /*
  27. ** Private data structures of amigancp.library
  28. */
  29.  
  30. struct NCP_Message
  31. {
  32.     struct Message m;
  33.     short cmd;
  34.     USHORT datasize;
  35.     void *data;
  36.     struct NCP_Channel *ncp_channel;
  37. };
  38.  
  39. struct NCP_Channel
  40. {
  41.     ULONG readsigmask,            /* Signalled when new PKT arrived (Mask!) */
  42.      writesigmask;                /* Signalled when PKT send (Mask!) */
  43.  
  44.     ULONG flags;
  45.  
  46.     /*** the following is private!!! ***/
  47.  
  48.     USHORT thischannel,            /* Channel allocation */
  49.      thatchannel;
  50.  
  51.     char thisname[12], thatname[12];    /* "Process" names */
  52.  
  53.     struct List incoming;        /* incoming PKTs */
  54.     struct List readbuffer;        /* incoming segments */
  55.  
  56.     struct MsgPort *readport, *writeport;
  57.     struct NCP_Message readmsg, writemsg;
  58.  
  59.     ULONG bytesread, byteswritten;
  60. };
  61.  
  62. #define NCPCF_ALLOC 1            /* Channel is allocated */
  63. #define NCPCF_ACTIVE 2            /* Connections exists */
  64. #define NCPCF_CONNREQ 4            /* Connection request underway */
  65. #define NCPCF_REMOTECLOSED 8    /* Closed by remote */
  66. #define NCPCF_OFFLINE    16        /* LLMAC is offline */
  67. #define NCPCF_XOFF        32        /* Data transmission held */
  68.  
  69. struct NCP_Info
  70. {
  71.     ULONG rt, mt;
  72.     UWORD remver;
  73.     UWORD conn;
  74.     ULONG skb, rkb;
  75. };
  76.  
  77. #include "rev.h"
  78. char version[]=
  79. {"$VER: AmigaNCP-Monitor " VERTAG};
  80.  
  81. static struct NCP_Channel *ncp;
  82. static struct Window *w;
  83. static struct RastPort *rp;
  84. static struct TextAttr top80 =
  85. {"topaz.font", 8, 0, 0};
  86. static struct MsgPort *infoport;
  87. static struct timerequest *treq;
  88. static struct NCP_Info ninfo;
  89. static struct DiskObject *diskobj;
  90.  
  91. extern struct WBStartup *_WBenchMsg;
  92.  
  93. /*** Requires 2.04 ++ ***/
  94. ULONG __oslibversion = 37;
  95.  
  96. /*** SAS/C cback.o startup */
  97. char *__procname = "NCP Monitor";
  98. ULONG __priority = -1;
  99. ULONG __stack = 4000;
  100. ULONG _BackGroundIO = FALSE;
  101.  
  102. #define text(x,y,s) Move(rp,x,y);Text(rp,s,strlen(s))
  103.  
  104. static void __stdargs 
  105. sprintf(char *to, char *fmt,...)
  106. {
  107.     static UWORD fmtfunc[]=
  108.     { 0x16c0, 0x4e75};
  109.  
  110.     RawDoFmt(fmt, &fmt + 1, (APTR) fmtfunc, to);
  111. }
  112.  
  113. void
  114. cleanup(void)
  115. {
  116.     if (diskobj)
  117.         FreeDiskObject(diskobj);
  118.     if (treq)
  119.     {
  120.         if (treq->tr_node.io_Device)
  121.             CloseDevice(treq);
  122.         DeleteIORequest(treq);
  123.     }
  124.     if (infoport)
  125.         DeletePort(infoport);
  126.     if (w)
  127.         CloseWindow(w);
  128. }
  129.  
  130. static void
  131. drawncp(int y, struct NCP_Channel *ncp)
  132. {
  133.     char buffer[80];
  134.  
  135.     sprintf(buffer, "%ld %-8.8s  %ld %-8.8s  %lc%lc%lc%lc%lc%lc  %10ld %10ld",
  136.             ncp->thischannel, ncp->thisname,
  137.             ncp->thatchannel, ncp->thatname,
  138.             (ncp->flags & NCPCF_ALLOC) ? 'A' : '-',
  139.             (ncp->flags & NCPCF_ACTIVE) ? 'A' : '-',
  140.             (ncp->flags & NCPCF_CONNREQ) ? 'C' : '-',
  141.             (ncp->flags & NCPCF_REMOTECLOSED) ? 'R' : '-',
  142.             (ncp->flags & NCPCF_OFFLINE) ? 'O' : '-',
  143.             (ncp->flags & NCPCF_XOFF) ? 'X' : '-',
  144.             ncp->byteswritten, ncp->bytesread);
  145.  
  146.     text(4, y, buffer);
  147. }
  148.  
  149. static char *
  150. datstr(time_t t)
  151. {
  152.     struct tm *tm = localtime(&t);
  153.     static char dat[64];
  154.  
  155.     if (!t)
  156.         return ("--            ");
  157.  
  158.     sprintf(dat, "%02ld:%02ld:%02ld %02ld-%02ld",
  159.             tm->tm_hour, tm->tm_min, tm->tm_sec,
  160.             tm->tm_mday, tm->tm_mon + 1);
  161.  
  162.     return (dat);
  163. }
  164.  
  165.  
  166. static void
  167. drawninfo(void)
  168. {
  169.     char buffer[80];
  170.  
  171.     NCP_private1(&ninfo);
  172.  
  173.     strcpy(buffer, datstr(ninfo.mt));
  174.     text(116, 79, buffer);
  175.  
  176.     strcpy(buffer, datstr(ninfo.rt));
  177.     text(116, 87, buffer);
  178.  
  179.     sprintf(buffer, "%ld  %9.9s", ninfo.remver, ninfo.conn ? "[Connect]" : NULL);
  180.     text(332, 87, buffer);
  181.  
  182.     sprintf(buffer, "%10ld %10ld", ninfo.skb, ninfo.rkb);
  183.     text(260, 79, buffer);
  184.  
  185. }
  186.  
  187. static void
  188. drawinfo(void)
  189. {
  190.     SetDrMd(rp, JAM2);
  191.     SetAPen(rp, 3);
  192.     RectFill(rp, 0, 0, 432, 90);
  193.  
  194.     SetBPen(rp, 3);
  195.     SetAPen(rp, 2);
  196.  
  197.     /* Infoline */
  198.     text(4, 8, "# ThisProc  # RemotePr  Status  Bytes Sent  Received");
  199.     text(4, 79, "Online since:                 =");
  200.     text(4, 87, "Remote NCP..:                 Version:");
  201.     Move(rp, 0, 10);
  202.     Draw(rp, 431, 10);
  203.     Move(rp, 0, 70);
  204.     Draw(rp, 431, 70);
  205.  
  206.     SetAPen(rp, 1);
  207. }
  208.  
  209. void __tzset( void )
  210. {
  211.     __timezone = 0;
  212. }
  213.  
  214. int 
  215. main(int argc, char **argv)
  216. {
  217.     struct IntuiMessage *im, imsg;
  218.     int c;
  219.     ULONG sigs, infosig, idcmpsig;
  220.     char windowtitle[ 128 ];
  221.     USHORT windowx = 0, windowy = 0;
  222.     char *iconname;
  223.     struct TextFont *tf;
  224.  
  225.     /* Are we already running? */
  226.     Forbid();
  227.     infoport = FindPort("NCP Monitor");
  228.     if (infoport)
  229.     {
  230.         Signal(infoport->mp_SigTask, SIGBREAKF_CTRL_F);
  231.         Permit();
  232.         return (0);
  233.     }
  234.     infoport = CreatePort("NCP Monitor", -128);
  235.     infosig = 1 << infoport->mp_SigBit;
  236.     Permit();
  237.  
  238.     onexit(cleanup);
  239.  
  240.     /*** Load Icon ***/
  241.     if (argc)
  242.         diskobj = GetDiskObjectNew(iconname = argv[0]  );
  243.     else
  244.         diskobj = GetDiskObjectNew(iconname = _WBenchMsg->sm_ArgList[0].wa_Name);
  245.  
  246.     if (diskobj)
  247.     {
  248.         char *def = FindToolType(diskobj->do_ToolTypes, "WINDOW");
  249.  
  250.         if (def)
  251.         {
  252.             windowx = atoi(def);
  253.             def = strchr(def, '/');
  254.             if (def++)
  255.             {
  256.                 windowy = atoi(def);
  257.             }
  258.         }
  259.     }
  260.  
  261.     sprintf(windowtitle, "AmigaNCP Monitor - amigancp.library v%ld.%ld [%s]",
  262.             NCPBase->lib_Version,
  263.             NCPBase->lib_Revision,
  264.             NCPBase->lib_IdString
  265.         );
  266.  
  267.     /*** Open Window ***/
  268.     w = OpenWindowTags(NULL,
  269.                        (windowx) ? WA_Left : TAG_IGNORE, windowx,
  270.                        (windowy) ? WA_Top : TAG_IGNORE, windowy,
  271.                        WA_InnerWidth, 432,
  272.                        WA_InnerHeight, 90,
  273.                        WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  274.                        WA_Flags, WINDOWDRAG | WINDOWDEPTH | RMBTRAP | GIMMEZEROZERO | WINDOWCLOSE | SIMPLE_REFRESH | WINDOWSIZING,
  275.                        WA_MinWidth, 120,
  276.                        WA_MinHeight, 25,
  277.                        WA_Title, windowtitle,
  278.                        WA_ScreenTitle, &version[ 6 ],
  279.                        WA_AutoAdjust, TRUE,
  280.                        TAG_DONE
  281.         );
  282.  
  283.     if (!w)
  284.     {
  285.         PutStr("can't open window");
  286.         return (20);
  287.     }
  288.  
  289.     treq = CreateIORequest( infoport, sizeof( *treq ) );
  290.     OpenDevice( "timer.device", UNIT_VBLANK, treq, 0 );
  291.  
  292.     idcmpsig = 1 << w->UserPort->mp_SigBit;
  293.  
  294.     rp = w->RPort;
  295.     tf = OpenFont( &top80 );
  296.     SetFont( rp, tf );
  297.  
  298.     drawinfo();
  299.  
  300.     ncp = NCP_private1(NULL);
  301.  
  302.     /*** Main Loop ***/
  303.     FOREVER
  304.     {
  305.         SetSignal(0, infosig);
  306.  
  307.         for (c = 0; c < 7; c++)
  308.             drawncp(19 + c * 8, &ncp[c]);
  309.  
  310.         drawninfo();
  311.  
  312.         treq->tr_node.io_Command = TR_ADDREQUEST;
  313.         treq->tr_time.tv_secs = ( w->Flags & WFLG_WINDOWACTIVE ) ? 1 : 3;
  314.         treq->tr_time.tv_micro = 0;
  315.  
  316.         SendIO(treq);
  317.  
  318.         sigs = Wait(infosig | idcmpsig | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F);
  319.  
  320.         /* Abort Timer */
  321.         AbortIO(treq);
  322.         WaitIO(treq);
  323.  
  324.         im = (struct IntuiMessage *) GetMsg(w->UserPort);
  325.         if (im)
  326.         {
  327.             imsg = *im;
  328.             ReplyMsg(im);
  329.             if (imsg.Class == CLOSEWINDOW)
  330.                 break;
  331.             else if (imsg.Class == REFRESHWINDOW)
  332.             {
  333.                 BeginRefresh(w);
  334.                 drawinfo();
  335.                 EndRefresh(w, TRUE);
  336.             }
  337.         }
  338.  
  339.         if (sigs & SIGBREAKF_CTRL_C)
  340.             break;
  341.  
  342.         if (sigs & SIGBREAKF_CTRL_F)
  343.             WindowToFront(w);
  344.     }
  345.  
  346.     /*** Save Icon ***/
  347.     if (diskobj)
  348.     {
  349.         char *tt[2];
  350.  
  351.         sprintf(windowtitle, "WINDOW=%ld/%ld",
  352.                 w->LeftEdge,
  353.                 w->TopEdge
  354.             );
  355.  
  356.         tt[0] = windowtitle;
  357.         tt[1] = NULL;
  358.  
  359.         diskobj->do_ToolTypes = tt;
  360.         PutDiskObject(iconname, diskobj);
  361.     }
  362.  
  363.     if( tf )
  364.         CloseFont( tf );
  365.  
  366.     /*** Exit ***/
  367.     return (0);
  368. }
  369.